package cn.com.elite;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.elite.mail.MailSend;
import cn.com.elite.mail.MailSend.SendTypeEnum;
import cn.com.elite.util.Util;

public class mailthread extends Thread{
	private static final Log log=LogFactory.getLog(mailthread.class);
	private String sql="select * from mailservice where datediff(day,UpdateTime,getdate())=0 and WorkFlag=1 and MailFlag!=1";
	private String address="tonyxu@elitecrm.com";
	private String ip="smtp.elitecrm.com";
	private String username="tonyxu@elitecrm.com";
	private String password="tony0308";
	private String toaddress="Brookechen@elitecrm.com;Roger.Luo@dominos.com.cn;Cloud.He@dominos.com.cn;helpdesk@dominos.com.cn;felixlin@yotor.com.tw;tonyxu@elitecrm.com;peterzhang@elitecrm.com;changeluo@elitecrm.com;michael.wang@dominos.com.cn";
	private String toaddress1="tonyxu@elitecrm.com";
		
	public void dorun()throws Exception {
		List list=mailservice.mailBean.chaxunList(sql);
		if(list!=null&&list.size()>0){
			for(int i=0;i<list.size();i++){
				Map map=(Map)list.get(i);
				String cityflag=(String)map.get("cityflag");
				MailSend mail=new MailSend();
				mail.setFromAddress(address);
				mail.setSMTP(ip,username,password);
				mail.setAddress(toaddress,SendTypeEnum.TO);
				if(cityflag.equals("BJ")){
					mail.setSubject("北京本日数据同步结果");//标题
					mail.setHtmlContext("北京本日数据同步成功完成");//内容
				}else if(cityflag.equals("SH")){
					mail.setSubject("上海本日数据同步结果");//标题
					mail.setHtmlContext("上海本日数据同步成功完成");//内容
				}			
				mail.sendBatch();
				log.info("update:"+mailservice.mailBean.update_sql("update mailservice set MailFlag=1 where cityflag='"+cityflag+"' and datediff(day,UpdateTime,getdate())=0"));
			}
		}else{ 
			log.debug("未同步完成");  
		}
		
	}
	public void run(){ 
		try{
			while(true){
				dorun();
				Thread.sleep(60000);
			}
		}catch(Exception e){
			log.error(Util.getERRO(e));
		}finally{
//			run();
		}
	}

}
